package com.cxs.shopstore.controllers;

import com.cxs.shopstore.config.ImgCodeConf;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

/*
 * @Author:cxs
 * @Date:2021/5/11  12:41
 * @Version 1.0
 * */
@RestController
@SuppressWarnings("all")
public class ImgCodeController {
    @Autowired
    private DefaultKaptcha defaultKaptcha;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private ImgCodeConf imgCodeConf;

    @GetMapping("/getImgCode")
    public Map<String,Object> defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws Exception {
        byte[] bytes = null;
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        Map<String,Object> map = new HashMap<>();
        try {
            // 生产验证码字符串并保存到session中,以后改redis
            String createText = defaultKaptcha.createText();
            //httpServletRequest.getSession().setAttribute("verificationCode", createText.toUpperCase(Locale.ROOT));
            String imgKey = UUID.randomUUID().toString().replaceAll("-","");
            //存入redis
            redisTemplate.opsForValue().set(imgCodeConf.getPrifex() + imgKey,createText.toUpperCase(Locale.ROOT));
            // 使用生成的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
            BufferedImage challenge = defaultKaptcha.createImage(createText);
            ImageIO.write(challenge, "jpg", jpegOutputStream);
            bytes = jpegOutputStream.toByteArray();
            map.put("code",200);
            map.put("key",imgKey);
            map.put("img",Base64.encode(bytes));
        } catch (IllegalArgumentException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
        //前后端不分离情况
        // 定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组
//        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
//        httpServletResponse.setHeader("Cache-Control", "no-store");
//        httpServletResponse.setHeader("Pragma", "no-cache");
//        httpServletResponse.setDateHeader("Expires", 0);
//        httpServletResponse.setContentType("image/jpeg");
//        ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
//        responseOutputStream.write(captchaChallengeAsJpeg);
//        responseOutputStream.flush();
//        responseOutputStream.close();
        return map;
    }
}
